/*
 * com.ziofront.utils
 * HangulUtils.java
 * Jiho Park 2009. 11. 27.
 *
 * Copyright (c) 2009 ziofront.com. All Rights Reserved.
 */
package com.lge.clock.worldclock.util;

/**
 * @author Jiho Park
 * 
 */
public class HangulUtils {

	/*
	 * 
	 * @param decimal
	 * 
	 * @return
	 */
	private static String toHexString(int decimal) {
		Long intDec = Long.valueOf(decimal);
		return Long.toHexString(intDec);
	}

	public static final int HANGUL_BEGIN_UNICODE = 44032; // 媛�
	public static final int HANGUL_END_UNICODE = 55203; // ��
	public static final int HANGUL_BASE_UNIT = 588;

	private static final int[] INITIAL_SOUND_UNICODE = { 12593, 12594, 12596,
			12599, 12600, 12601, 12609, 12610, 12611, 12613, 12614, 12615,
			12616, 12617, 12618, 12619, 12620, 12621, 12622 };

//	public static final char[] INITIAL_SOUND = { '��, '��, '��, '��, '��, '��,
//			'��, '��, '��, '��, '��, '��, '��, '��, '��, '��, '��, '��, '�� };
	private static final char[] INITIAL_SOUND = { 0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139,
		0x3141, 0x3142, 0x3143, 0x3145, 0x3146, 0x3147, 0x3148, 0x3149, 0x314A, 0x314B, 0x314C, 0x314D, 0x314E };

	/**
	 * 臾몄옄瑜��좊땲肄붾뱶(10吏꾩닔)濡�蹂�솚 ��諛섑솚 �쒕떎.
	 * 
	 * @param ch
	 * @return
	 */
	public static int convertCharToUnicode(char ch) {
		return (int) ch;
	}

	/**
	 * 臾몄옄�댁쓣 �좊땲肄붾뱶(10吏꾩닔)濡�蹂�솚 ��諛섑솚 �쒕떎.
	 * 
	 * @param str
	 * @return
	 */
	public static int[] convertStringToUnicode(String str) {

		int[] unicodeList = null;

		if (str != null) {
			unicodeList = new int[str.length()];
			for (int i = 0; i < str.length(); i++) {
				unicodeList[i] = convertCharToUnicode(str.charAt(i));
			}
		}

		return unicodeList;
	}

	/**
	 * �좊땲肄붾뱶(16吏꾩닔)瑜�臾몄옄濡�蹂�솚 ��諛섑솚 �쒕떎.
	 * 
	 * @param hexUnicode
	 * @return
	 */
	public static char convertUnicodeToChar(String hexUnicode) {
		return (char) Integer.parseInt(hexUnicode, 16);
	}

	/**
	 * �좊땲肄붾뱶(10吏꾩닔)瑜�臾몄옄濡�蹂�솚 ��諛섑솚 �쒕떎.
	 * 
	 * @param unicode
	 * @return
	 */
	public static char convertUnicodeToChar(int unicode) {
		return convertUnicodeToChar(toHexString(unicode));
	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	public static String getHangulInitialSound(String value) {

		StringBuffer result = new StringBuffer(16);

		int[] unicodeList = convertStringToUnicode(value);
		
		if(unicodeList == null) {
			return null;
		}
		
		for (int unicode : unicodeList) {

			if (HANGUL_BEGIN_UNICODE <= unicode
					&& unicode <= HANGUL_END_UNICODE) {
				int tmp = (unicode - HANGUL_BEGIN_UNICODE);
				int index = tmp / HANGUL_BASE_UNIT;
				result.append(INITIAL_SOUND[index]);
			} else {
				result.append(convertUnicodeToChar(unicode));

			}
		}

		return result.toString();
	}

	public static boolean[] getIsChoSungList(String name) {
		if (name == null) {
			return null;
		}

		boolean[] choList = new boolean[name.length()];

		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);

			boolean isCho = false;
			for (char cho : INITIAL_SOUND) {
				if (c == cho) {
					isCho = true;
					break;
				}
			}

			choList[i] = isCho;

		}

		return choList;
	}

	public static String getHangulInitialSound(int position, String value,
			String searchKeyword) {
		boolean[] isChoSungList = new boolean[position + searchKeyword.length()];
		boolean[] isChoSungListbuf = getIsChoSungList(searchKeyword);
		
		if(isChoSungListbuf == null) {
			return null;
		}
		
		for(int i = 0; i < isChoSungList.length; i++)
		{
			if(i < position)
			{
				isChoSungList[i] = false;
			}
			else
			{
				isChoSungList[i] = isChoSungListbuf[i - position];
			}
		}
		return getHangulInitialSound(position, value, isChoSungList);
	}

	public static String getHangulInitialSound(int position, String value, boolean[] isChoList) {

		StringBuffer result = new StringBuffer(16);

		int[] unicodeList = convertStringToUnicode(value);
		
		
		if(unicodeList == null) {
			return null;
		}
		
		for (int idx = position; idx < unicodeList.length; idx++) {
			int unicode = unicodeList[idx];

			if (isChoList != null && idx <= (isChoList.length - 1)) {
				if (isChoList[idx]) {
					if (HANGUL_BEGIN_UNICODE <= unicode
							&& unicode <= HANGUL_END_UNICODE) {
						int tmp = (unicode - HANGUL_BEGIN_UNICODE);
						int index = tmp / HANGUL_BASE_UNIT;
						result.append(INITIAL_SOUND[index]);
					} else {
						result.append(convertUnicodeToChar(unicode));
					}
				} else {
					result.append(convertUnicodeToChar(unicode));
				}
			} else {
				result.append(convertUnicodeToChar(unicode));
			}
		}

		return result.toString();
	}
}
